<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class seminar2 extends CI_Controller {
	
	function __construct(){
		parent::__construct();
		$this->load->model('mdl_seminar2');
		$this->load->helper('download');
		//$this->output->enable_profiler(TRUE);
	}
	/*function test(){
		$date = date("Y-m-d");// current date

		$date = date('Y-m-d', strtotime("+6 months", strtotime($date)));
		
		echo $date;
	}*/
	
	function index(){
		$this->load->view('/ta/seminar2');
	}
	
	
	function grid(){
		$data = $this->mdl_seminar2->getdata();
		echo $this->mdl_seminar2->togrid($data['row_data'], $data['row_count']);
	}
	
	function add(){
		$data['id'] = '';
		$data['tahun_id'] = '';
		$data['nim'] = '';
		$data['judul_ta'] = '';
		$data['abstrak'] = '';
		$data['pembimbing1'] = '';
		$data['pembimbing2'] = '';
		//$data['seminar'] = '';
		$data['keterangan'] = '';
		$data['tgl_seminar2'] = FormatDateFromMysql(date('Y-m-d'));
		$data['aksi'] = 'add';
		$this->load->view('/ta/seminar2_form', $data);
	}
	
	function edit($kode){
		$r = $this->mdl_seminar2->getdataedit($kode);
		
		$data['id'] = $kode;
		$data['tahun_id'] = $r->row()->tahun_id;
		$data['nim'] = $r->row()->nim;
		$data['judul_ta'] = $r->row()->judul_ta;
		$data['abstrak'] = $r->row()->abstrak;
		$data['kd_fakultas'] = $r->row()->kd_fakultas;
		$data['kd_prodi'] = $r->row()->kd_prodi;
		$data['pembimbing1'] = $r->row()->pembimbing1;
		$data['pembimbing2'] = $r->row()->pembimbing2;
		//$data['seminar'] = $r->row()->seminar;
		$data['keterangan'] = $r->row()->keterangan;
		$data['tgl_seminar2'] = FormatDateFromMysql($r->row()->tgl_seminar2);
		$data['aksi'] = 'edit';
		$this->load->view('/ta/seminar2_form', $data);
	}
	
	
	function save($aksi){
		# init
		$status = "";
		$result = false;
		$data['pesan_error'] = '';
		
		# get post data
		foreach($_POST as $key => $value){
			$data[$key] = $value;
		}
		$data['file_upload'] = '';
		
		
		# upload file
			if (!empty($_FILES['userfile']['name'])){ // jika ada file yg akan diupload
				$config['upload_path'] = './file_upload/proposal/';
				$config['allowed_types'] = 'pdf';
				$config['overwrite'] = TRUE;
				$config['max_size']	= '20000';
				$this->load->library('upload', $config);
				
				if (!$this->upload->do_upload()){
					// $error = array('error' => $this->upload->display_errors());
					// $this->load->view('form_upload', $error);
				}else{
					$up = $this->upload->data();
					$data['file_upload'] = $up['file_name'];
				}
			}
			if($aksi=="add"){ // add
				# rules validasi form
				
				$this->form_validation->set_rules("tahun_id", 'Tahun Akademik', 'trim|required|xss_clean');
				$this->form_validation->set_rules("nim", 'NIM', 'trim|required|xss_clean');
				//$this->form_validation->set_rules("pembimbing1", 'Pembimbing 1', 'trim|required|xss_clean');
				//$this->form_validation->set_rules("pembimbing2", 'Pembimbing 2', 'trim|required|xss_clean');
				$this->form_validation->set_rules("tgl_seminar2", 'Tanggal Seminar', 'trim|required|xss_clean');
				
				
				# message rules
				$this->form_validation->set_message('required', 'Field %s harus diisi.');

				$data['pesan_error'] = '';
				if ($this->form_validation->run() == FALSE){
					//$data["pesan_error"] .= trim(form_error('kd_biaya',' ',' '))==''?'':form_error('kd_biaya',' ',' ').'<br>';
					$data["pesan_error"] .= trim(validation_errors(' ',' '))==''?'':validation_errors(' ',' ');
				}else{
					//$result = !$this->mdl_seminar2->isExistKode($data['nim']);
					$result = !$this->mdl_seminar2->isExistKode($data);
					if ($result){
						$result = $this->mdl_seminar2->InsertOnDb($data);
					}else{
						$data['pesan_error'] .= 'Sudah terdapat nim dalam database!';
					}
					//$result = $this->mdl_seminar2->InsertOnDb($data);
				}
			}else { // edit
				$result=$this->mdl_seminar2->UpdateOnDb($data);
			}
		
		
		if($result){
			echo json_encode(array('success'=>true));
		}else{
			echo json_encode(array('msg'=>$data['pesan_error']));
		}
		
	} 
	// fungsi delet dengan mengganti status A menjadi D
	function delete($data){
		$result = $this->mdl_seminar2->DeleteOnDb($data);
		if ($result){
			echo json_encode(array('success'=>true));
		} else {
			echo json_encode(array('msg'=>'Data gagal dihapus'));
		}
	} 
	
	function detail($kode){
		$r = $this->mdl_seminar2->getdataedit($kode);
		
		//$data = file_get_contents("/path/to/photo.jpg"); // Read the file's contents
		//s$name = 'myphoto.jpg';

		//force_download($name, $data);
		
		$data['id'] = $kode;
		$data['tahun_id'] = $r->row()->tahun_id;
		$data['nim'] = $r->row()->nim;
		$data['nama'] = $r->row()->nama;
		$data['judul_ta'] = $r->row()->judul_ta;
		$data['abstrak'] = $r->row()->abstrak;
		//$data['file_seminar'] = $r->row()->file_seminar;
		$data['kd_fakultas'] = $r->row()->kd_fakultas;
		$data['kd_prodi'] = $r->row()->kd_prodi;
		$data['pembimbing1'] = $r->row()->pembimbing1;
		$data['pembimbing2'] = $r->row()->pembimbing2;
		//$data['seminar'] = $r->row()->seminar;
		$data['keterangan'] = $r->row()->keterangan;
		$data['tgl_seminar2'] = FormatDateFromMysql($r->row()->tgl_seminar2);
		$data['aksi'] = 'detail';
		$this->load->view('/ta/seminar2_detail', $data);
	}
	//fungsi delet record di database
	/*function delete($kode){
		$result = $this->mdl_seminar2->DeleteOnDb($kode);
		if ($result){
			echo json_encode(array('success'=>true));
		} else {
			echo json_encode(array('msg'=>'Data gagal dihapus'));
		}
	} */
}